﻿namespace NJQuery {
	using System;

	internal static class Utility {

		internal static bool TryParse(string text, out int? result) {
			return TryParse(text, int.TryParse, out result);
		}

		internal static bool TryParse(string text, out DateTime? result) {
			return TryParse(text, DateTime.TryParse, out result);
		}

		private delegate bool TryParseDelegate<T>(string text, out T result) where T : struct;
		private static bool TryParse<T>(string text, TryParseDelegate<T> tryParse, out Nullable<T> result) where T : struct {
			if(string.IsNullOrEmpty(text)) {
				result = null;
				return true;
			}

			T value;
			bool success = tryParse(text, out value);

			result = value;
			return success;
		}

	}

}